package org.john.common.vo;

import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.john.common.util.ObjectUtil;

@Getter
@Setter
public class FtpConfig {

    private String ip;
    private int port;
    private String username;
    private String password;
    private String defaultRemoteDir;
    private String encoding = "gbk";
    private boolean passiveMode = true;

    public FtpConfig() {

    }

    /**
     * ip=192.168.120.152;port=21;username=irmds;password=aaaaaa;defaultDir=/ attachment;encoding=utf-8 <br />
     * 以分号分隔的key=value的字符串, 顺序可以不固定, 但key值要保持一致 ip,port,username,password,defaultDir,encoding <br />
     */
    public FtpConfig(String formatString) {
        if (StringUtils.isNotEmpty(formatString)) {
            String[] configs = formatString.split(";");
            if (ArrayUtils.isNotEmpty(configs)) {
                for (String config : configs) {
                    String key = config.split("=")[0];
                    String value = config.split("=")[1];
                    if ("ip".equalsIgnoreCase(key)) {
                        this.ip = value;
                    } else if ("port".equalsIgnoreCase(key)) {
                        this.port = Integer.parseInt(value);
                    } else if ("username".equalsIgnoreCase(key)) {
                        this.username = value;
                    } else if ("password".equalsIgnoreCase(key)) {
                        this.password = value;
                    } else if ("root".equalsIgnoreCase(key) || "defaultDir".equalsIgnoreCase(key) || "defaultRemoteDir".equalsIgnoreCase(key)) {
                        this.defaultRemoteDir = value;
                    } else if ("encoding".equalsIgnoreCase(key)) {
                        this.encoding = value;
                    } else if ("passiveMode".equalsIgnoreCase(key)) {
                        this.passiveMode = ObjectUtil.asPrimitiveBoolean(value, true);
                    }
                }
            }
        }
    }

    public boolean initFinished() {
        return StringUtils.isNotEmpty(ip) && (0 < port) && StringUtils.isNotEmpty(username) && StringUtils.isNotEmpty(password) && StringUtils.isNotEmpty(encoding);
    }

    @Override
    public String toString() {
        ToStringBuilder tsBuilder = new ToStringBuilder(this);
        tsBuilder.append("ip=" + ip);
        tsBuilder.append("port=" + port);
        tsBuilder.append("username=" + username);
        tsBuilder.append("password=" + password);
        tsBuilder.append("defaultRemoteDir=" + defaultRemoteDir);
        tsBuilder.append("encoding=" + encoding);
        tsBuilder.append("passiveMode=" + passiveMode);
        return tsBuilder.toString();
    }

    public boolean compareByBasicProperties(FtpConfig otherFtpConfig) {
        return StringUtils.equals(ip, otherFtpConfig.getIp()) //
                && port == otherFtpConfig.getPort() //
                && StringUtils.equals(username, otherFtpConfig.getUsername()) //
                && StringUtils.equals(password, otherFtpConfig.getPassword()); //
    }

    public String generateFtpPrefix() {
        return "ftp://" + this.username + ":" + this.password + "@" + this.ip + ":" + this.port;
    }
}